﻿using Microsoft.AspNet.Mvc.Facebook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// Add any fields you want to be saved for each user and specify the field name in the JSON coming back from Facebook
// http://go.microsoft.com/fwlink/?LinkId=301877

namespace MakeWeekConcerts.Models
{
    public class SimpleFacebookFriend
    {
        public string Id { get; set; }
        public string Gender { get; set; }
        public string Name { get; set; }

        [FacebookFieldModifier("height(100).width(100)")] // This sets the picture height and width to 100px.
        public FacebookConnection<FacebookPicture> Picture { get; set; }
    }

    public class FacebookFriend
    {
        public string Id { get; set; }
        public string Gender { get; set; }
        public string Name { get; set; }
        public string Link { get; set; }

        [FacebookFieldModifier("height(100).width(100)")] // This sets the picture height and width to 100px.
        public FacebookConnection<FacebookPicture> Picture { get; set; }

        public FacebookGroupConnection<FacebookLike> Music { get; set; }

        public async Task<Tuple<List<string>, Dictionary<string, string>>> FindArtistOverlap(FacebookUser myself, double minimumSimilarityThreshold = 0.3)
        {
            var myArtists = new HashSet<string>(myself.Music.Data.Select(x => x.Name), StringComparer.OrdinalIgnoreCase);
            var theirArtists = new HashSet<string>(this.Music.Data.Select(x => x.Name), StringComparer.OrdinalIgnoreCase);
            var overlap = myArtists.Intersect(theirArtists).ToList();

            var mySimilar = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            foreach (var artist in myArtists)
            {
                var similar = await DataLoader.FindSimilarArtistsAsync(artist);
                similar.SimilarArtists.Artists.Where(x => x.Match > minimumSimilarityThreshold).Select(x => x.Name).ToList().ForEach(x => mySimilar[x] = artist);
            }

            var similarOverlap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            foreach (var artist in theirArtists)
            {
                string similar;
                if (mySimilar.TryGetValue(artist, out similar))
                {
                    similarOverlap[artist] = similar;
                }
            }

            return Tuple.Create(overlap, similarOverlap);
        }
    }

    public class FacebookLike
    {
        public string Category { get; set; }
        public string Name { get; set; }
    }
}
